﻿using JianZhangTool.tool;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;

namespace JianZhangTool.statistics.excel
{
    public class ExcelInput
    {
        /// <summary>
        /// 从文件中读取舰长信息
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static List<GuardInfo> ParseFromFile(string filename)
        {
            FileStream fs = null;
            IWorkbook workbook = null;
            try
            {
                using (fs = File.OpenRead(filename))
                {
                    if (filename.EndsWith(".xlsx"))
                    {
                        workbook = new XSSFWorkbook(fs);
                    }
                    else if (filename.EndsWith(".xls"))
                    {
                        workbook = new HSSFWorkbook(fs);
                    }
                    else
                    {
                        return null;
                    }
                    if (workbook != null)
                    {
                        return ParseFromSheet(workbook.GetSheetAt(0));
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                // 最终关闭文件流
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 从Excel页中读取数据
        /// </summary>
        /// <param name="sheet"></param>
        /// <returns></returns>
        private static List<GuardInfo> ParseFromSheet(ISheet sheet)
        {
            if (sheet == null)
            {
                return null;
            }
            try
            {
                IRow headerRow = sheet.GetRow(sheet.FirstRowNum);
                int index_uid = -1;
                int index_uname = -1;
                int index_oldnames = -1;
                int index_jzc = -1;
                int index_tdc = -1;
                int index_zdc = -1;
                int index_syc = -1;
                for (int i = 0; i < headerRow.LastCellNum; i++)
                {
                    switch (headerRow.GetCell(i).StringCellValue)
                    {
                        case "UID":
                            {
                                index_uid = i;
                            }
                            break;
                        case "用户名":
                            {
                                index_uname = i;
                            }
                            break;
                        case "曾用名":
                            {
                                index_oldnames = i;
                            }
                            break;
                        case "舰长次数":
                            {
                                index_jzc = i;
                            }
                            break;
                        case "提督次数":
                            {
                                index_tdc = i;
                            }
                            break;
                        case "总督次数":
                            {
                                index_zdc = i;
                            }
                            break;
                        case "深渊次数":
                            {
                                index_syc = i;
                            }
                            break;
                        default:
                            break;
                    }
                }
                // UID和用户名为必须
                if (index_uid < 0 || index_uname < 0)
                {
                    return null;
                }

                // 读取剩余行
                List<GuardInfo> infos = new List<GuardInfo>();
                for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                {
                    IRow dataRow = sheet.GetRow(i);
                    if (dataRow == null)
                    {
                        continue;
                    }
                    GuardInfo guardInfo = new GuardInfo();
                    guardInfo.uid = int.Parse(Tools.get_cell_str(dataRow.GetCell(index_uid)));
                    guardInfo.uname = Tools.get_cell_str(dataRow.GetCell(index_uname));
                    if (index_oldnames >= 0)
                    {
                        string str = Tools.get_cell_str(dataRow.GetCell(index_oldnames));
                        if (str != null && str.Length > 0)
                        {
                            string[] splits = str.Split(',');
                            foreach (string on in splits)
                            {
                                guardInfo.old_unames.Add(on);
                            }
                        }
                    }
                    if (index_jzc >= 0)
                    {
                        string str = Tools.get_cell_str(dataRow.GetCell(index_jzc));
                        if (str != null && str.Length > 0)
                        {
                            try
                            {
                                guardInfo.jianzhang_num = int.Parse(str);
                            }
                            catch (Exception)
                            {
                                // do nothing
                            }
                        }
                    }
                    if (index_tdc >= 0)
                    {
                        string str = Tools.get_cell_str(dataRow.GetCell(index_tdc));
                        if (str != null && str.Length > 0)
                        {
                            try
                            {
                                guardInfo.tidu_num = int.Parse(str);
                            }
                            catch (Exception)
                            {
                                // do nothing
                            }
                        }
                    }
                    if (index_zdc >= 0)
                    {
                        string str = Tools.get_cell_str(dataRow.GetCell(index_zdc));
                        if (str != null && str.Length > 0)
                        {
                            try
                            {
                                guardInfo.zongdu_num = int.Parse(str);
                            }
                            catch (Exception)
                            {
                                // do nothing
                            }
                        }
                    }
                    if (index_syc >= 0)
                    {
                        string str = Tools.get_cell_str(dataRow.GetCell(index_syc));
                        if (str != null && str.Length > 0)
                        {
                            try
                            {
                                guardInfo.shenyuan_num = int.Parse(str);
                            }
                            catch (Exception)
                            {
                                // do nothing
                            }
                        }
                    }
                    infos.Add(guardInfo);
                }
                return infos;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
